package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Apl_condeploy_person;
import com.power.sifa.correction.model.pageInfo.Apl_condeploy_personPageInfo;
import com.power.sifa.correction.api.IApl_condeploy_personFeign;
import com.power.sifa.correction.service.Apl_condeploy_personService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   社区矫正人员布控人员信息控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_condeploy_personController",tags={"社区矫正人员布控人员信息"})
public class Apl_condeploy_personController implements IApl_condeploy_personFeign{

    @Resource
    private Apl_condeploy_personService apl_condeploy_personService;


    @Override
    @ApiOperation("查询社区矫正人员布控人员信息分页列表")
    @SaCheckPermission("apl_condeploy_person_list")
    public ResponseDTO list(Apl_condeploy_personPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_condeploy_person> page = this.apl_condeploy_personService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_condeploy_person apl_condeploy_person : page) {
                apl_condeploy_person.geneText();
                list.add(apl_condeploy_person);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看社区矫正人员布控人员信息")
    @SaCheckPermission("apl_condeploy_person_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_condeploy_person apl_condeploy_person  = apl_condeploy_personService.getById(id);
        if (apl_condeploy_person != null) {
            apl_condeploy_person.geneText();
        }
        return ResponseDTO.succData(apl_condeploy_person);
    }

    @Override
    @ApiOperation("删除社区矫正人员布控人员信息")
    @SaCheckPermission("apl_condeploy_person_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_condeploy_personService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改社区矫正人员布控人员信息")
    @SaCheckPermission("apl_condeploy_person_save")
    public ResponseDTO save(Apl_condeploy_person apl_condeploy_person) {
        Assert.notNull(apl_condeploy_person, "apl_condeploy_person不能为空");
        apl_condeploy_personService.saveOrUpdate(apl_condeploy_person);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除社区矫正人员布控人员信息")
    @SaCheckPermission("apl_condeploy_person_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_condeploy_personService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_condeploy_person_list")
    public void output(HttpServletResponse response, Apl_condeploy_personPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "社区矫正人员布控人员信息";
        String[] heads = {"序号","姓名","性别","矫正类别","罪名","身份证号","矫正期限","矫正开始日期","矫正结束日期","头像","行政区划代码","所属司法所id","管控级别","联系号码","民族","出生日期","入矫日期","运营商类型","家庭住址","户籍地","原判刑罚","入矫类别","曾用名","执行地","附加刑","布控时间","布控编号"};
        String[] fields = {"id","name","gender","crctype","crime","cardno","timelong","startdate","enddate","avatar","adid","deptid","controltype","phone","nation","birthym","enterdate","operators","addr","domicile","penalty","entertype","oncename","exeaddr","addpenalty","condate","concode"};
        Page<Apl_condeploy_person> page = this.apl_condeploy_personService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "社区矫正人员布控人员信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","姓名","性别","矫正类别","罪名","身份证号","矫正期限","矫正开始日期","矫正结束日期","头像","行政区划代码","所属司法所id","管控级别","联系号码","民族","出生日期","入矫日期","运营商类型","家庭住址","户籍地","原判刑罚","入矫类别","曾用名","执行地","附加刑","布控时间","布控编号"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","gender","crctype","crime","cardno","timelong","startdate","enddate","avatar","adid","deptid","controltype","phone","nation","birthym","enterdate","operators","addr","domicile","penalty","entertype","oncename","exeaddr","addpenalty","condate","concode"};
        List<Apl_condeploy_person> list = (List<Apl_condeploy_person>)ExcelUtil.Import(fields, file, Apl_condeploy_person.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_condeploy_personService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO findCondeployInfo(String id, String applyid) {
        try {
            return ResponseDTO.succData(this.apl_condeploy_personService.findCondeployInfo(id, applyid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/apl_condeploy_person/findCondeployInfo 查询社矫业务人员信息失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询社矫业务人员信息失败");
        }

    }

    @Override
    public ResponseDTO findList(Apl_condeploy_personPageInfo pageInfo) {
        try {
            return ResponseDTO.succData(this.apl_condeploy_personService.findList(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/apl_condeploy_person/findList 查询审批社矫人员信息失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询审批社矫人员信息失败");
        }

    }

}
